package com.bjut.vendormgrsys.web.rest;

import com.bjut.vendormgrsys.service.PositionService;
import com.bjut.vendormgrsys.service.UserService;
import com.bjut.vendormgrsys.support.bo.UserBO;
import com.bjut.vendormgrsys.web.dto.other.Body4;
import com.bjut.vendormgrsys.web.dto.other.Body5;
import com.bjut.vendormgrsys.web.dto.other.PositionInfo;
import com.bjut.vendormgrsys.web.dto.other.PositionSearch;
import com.bjut.vendormgrsys.web.dto.other.ResultPositionInfo;
import com.bjut.vendormgrsys.web.dto.other.ResultPositionInfoList;
import com.bjut.vendormgrsys.web.dto.other.ResultPositionInfoListData;
import com.bjut.vendormgrsys.web.dto.ResultStatusList;
import com.bjut.vendormgrsys.web.dto.Status;
import com.bjut.vendormgrsys.web.dto.Success;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
public class PositionApiController implements PositionApi {

    private static final Logger log = LoggerFactory.getLogger(PositionApiController.class);

    private final PositionService service;

    private final UserService userService;

    @org.springframework.beans.factory.annotation.Autowired
    public PositionApiController(PositionService service, UserService userService) {
        this.service = service;
        this.userService = userService;
    }

    @Secured({"ROLE_ADMIN", "ROLE_HRBP"})
    public ResponseEntity<Success> positionChangeStatusPost(@Parameter(in = ParameterIn.DEFAULT, schema = @Schema()) @Valid @RequestBody List<Body5> body) {
        ResponseEntity<Success> successEntity;
        try {
            service.changeStatus(body);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.OK.value()).message("修改职位状态成功"), HttpStatus.OK);
        } catch (Exception e) {
            log.error("修改职位状态异常：", e);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("修改职位状态失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return successEntity;
    }

    public ResponseEntity<Success> positionCreatePost(@Parameter(in = ParameterIn.DEFAULT, required = true, schema = @Schema()) @Valid @RequestBody PositionInfo body) {
        ResponseEntity<Success> successEntity;
        try {
            service.validPositionInfo(body);
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            service.savePositionPO(body, (UserBO) authentication.getPrincipal());
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.OK.value()).message("保存职位成功"), HttpStatus.OK);
        } catch (Exception e) {
            log.error("保存职位异常：", e);
            successEntity = new ResponseEntity<>(new Success().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("保存职位失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return successEntity;
    }

    public ResponseEntity<ResultPositionInfo> positionPositionInfoPost(@Parameter(in = ParameterIn.DEFAULT, required = true, schema = @Schema()) @Valid @RequestBody Body4 body) {
        ResponseEntity<ResultPositionInfo> responseEntity;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO principal = (UserBO) authentication.getPrincipal();
            PositionInfo data = service.findById(body.getPositionId(), principal);
            data.setCreator(userService.getUserInfoByAccount(data.getPrincipal()));
            responseEntity = new ResponseEntity<>(new ResultPositionInfo().code(HttpStatus.OK.value()).message("成功").data(data), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询职位详情异常：", e);
            responseEntity = new ResponseEntity<>(new ResultPositionInfo().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询职位详情失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }

    public ResponseEntity<ResultPositionInfoList> positionPositionListPost(@Parameter(in = ParameterIn.DEFAULT, schema = @Schema()) @Valid @RequestBody PositionSearch body) {
        ResponseEntity<ResultPositionInfoList> responseEntity;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO principal = (UserBO) authentication.getPrincipal();
            List<PositionInfo> items = new ArrayList<>();
            long totalCount = service.findAll(body, items, principal);
            for (PositionInfo data : items) {
                data.setCreator(userService.getUserInfoByAccount(data.getPrincipal()));
            }
            responseEntity = new ResponseEntity<>(new ResultPositionInfoList().code(HttpStatus.OK.value()).message("成功").data(new ResultPositionInfoListData().totalCount(totalCount).items(items)), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询职位详情异常：", e);
            responseEntity = new ResponseEntity<>(new ResultPositionInfoList().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询职位详情失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }

    public ResponseEntity<ResultStatusList> positionStatusListGet() {
        ResponseEntity<ResultStatusList> responseEntity;
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            UserBO principal = (UserBO) authentication.getPrincipal();
            List<Status> data = service.findAllStatus(principal);
            responseEntity = new ResponseEntity<>(new ResultStatusList().code(HttpStatus.OK.value()).message("成功").data(data), HttpStatus.OK);
        } catch (Exception e) {
            log.error("查询职位状态列表异常：", e);
            responseEntity = new ResponseEntity<>(new ResultStatusList().code(HttpStatus.INTERNAL_SERVER_ERROR.value()).message("查询职位状态列表失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return responseEntity;
    }
}
